package cn.threewaters.qtyy.web.controller.business;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import cn.threewaters.qtyy.common.bean.PageBean;
import cn.threewaters.qtyy.common.bean.ResponseJson;
import cn.threewaters.qtyy.dic.one.queryparam.QueryLq;
import cn.threewaters.qtyy.dic.one.queryresult.ResultLq;
import cn.threewaters.qtyy.service.business.LqService;
import cn.threewaters.qtyy.web.facade.Point;
import cn.threewaters.qtyy.web.facade.TempAnaly;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "粮情")
@Controller
@RequestMapping(value = "/lq")
public class LqController {
	@Autowired
	private LqService lqService;
	@Resource
	@Qualifier("oneJdbcTemplate")
	private JdbcTemplate oneJdbcTemplate;

	@ApiIgnore
	@ApiOperation(value = "测温")
	@RequestMapping(value = "/cw/main", method = RequestMethod.GET)
	public ModelAndView cfglMain(ModelMap modelMap) {
		return new ModelAndView("business/liangqing/cw", modelMap);
	}

	@ApiIgnore
	@ApiOperation(value = "数据详情")
	@RequestMapping(value = "/sjxq/main", method = RequestMethod.GET)
	public ModelAndView cfxqMain(ModelMap modelMap, String id) {
		modelMap.put("id", id);
		return new ModelAndView("business/liangqing/sjxq", modelMap);
	}

	@ApiIgnore
	@ApiOperation(value = "数据汇总")
	@RequestMapping(value = "/sjhz/main", method = RequestMethod.GET)
	public ModelAndView cfhzMain(ModelMap modelMap) {
		return new ModelAndView("business/liangqing/sjhz", modelMap);
	}

	@ApiIgnore
	@ApiOperation(value = "实时数据")
	@RequestMapping(value = "/ss/main", method = RequestMethod.GET)
	public ModelAndView ssMain(ModelMap modelMap, String storehousecode) {
		modelMap.put("storehousecode", storehousecode);
		return new ModelAndView("business/liangqing/ss", modelMap);
	}

	@ApiOperation(value = "概览")
	@RequestMapping(value = "/GLlist", method = RequestMethod.GET)
	@ResponseBody
	public ResponseJson getGLList(QueryLq queryLq) throws Exception {
		List<ResultLq> lqGLs = lqService.queryLqGL(queryLq);
		return ResponseJson.builder().success(true).obj(lqGLs).build();
	}

	@ApiOperation(value = "详情")
	@RequestMapping(value = "/XQlist", method = RequestMethod.GET)
	@ResponseBody
	public ResponseJson getXQList(QueryLq queryLq) throws Exception {
		Pagination pagination = new Pagination();
		pagination.setCurrent(1);
		pagination.setSize(100);
		List<ResultLq> lqXQs = lqService.queryLqXQ(queryLq, pagination);
		ResultLq resultLq = new ResultLq();
		if (lqXQs.size() > 0) {
			resultLq = lqXQs.get(0);
			String tempdata = resultLq.getTempdata();
			TempAnaly tempAnaly = new TempAnaly();
			Point[][][] analysis = tempAnaly.analysis(tempdata,
					Integer.parseInt(resultLq.getCablerow() == null ? "0" : resultLq.getCablerow()),
					Integer.parseInt(resultLq.getCablecolum() == null ? "0" : resultLq.getCablecolum()),
					Integer.parseInt(resultLq.getCabletier() == null ? "0" : resultLq.getCabletier()),
					resultLq.getStapoint(), resultLq.getSortdir(), resultLq.getPointrule(), resultLq.getSortrule());
			resultLq.setPoints(analysis);

			List<Point> pointList = Lists.newArrayList();
			for (int i = 0; i < analysis.length; i++) {
				for (int j = 0; j < analysis[i].length; j++) {
					for (int k = 0; k < analysis[i][j].length; k++) {
						pointList.add(analysis[i][j][k]);
					}
				}
			}

			for (int k = 0; k < analysis[0][0].length; k++) {
				double high = 0;
				double low = 0;
				double sum = 0;
				double index = 0;
				for (int i = 0; i < analysis.length; i++) {
					for (int j = 0; j < analysis[i].length; j++) {
						double parseDouble = Double
								.parseDouble(analysis[i][j][k].getTemp() == null ? "0" : analysis[i][j][k].getTemp());
						if (high < parseDouble && parseDouble != 1001) {
							high = parseDouble;
						}
						if (low > parseDouble || low == 0) {
							low = parseDouble;
						}
						sum = sum + parseDouble;
						index = index + 1;
					}
				}
				DecimalFormat df = new DecimalFormat("######0.0");
				if (k == 0) {
					resultLq.setOneavg(String.valueOf(df.format(sum / index)));
					resultLq.setOnehigh(String.valueOf(high));
					resultLq.setOnelow(String.valueOf(low));
				}
				if (k == 1) {
					resultLq.setTwoavg(String.valueOf(df.format(sum / index)));
					resultLq.setTwohigh(String.valueOf(high));
					resultLq.setTwolow(String.valueOf(low));
				}
				if (k == 2) {
					resultLq.setThreeavg(String.valueOf(df.format(sum / index)));
					resultLq.setThreehigh(String.valueOf(high));
					resultLq.setThreelow(String.valueOf(low));
				}
				if (k == 3) {
					resultLq.setFouravg(String.valueOf(df.format(sum / index)));
					resultLq.setFourhigh(String.valueOf(high));
					resultLq.setFourlow(String.valueOf(low));
				}
			}

			resultLq.setPointList(pointList);
		}
		List<Map<String, Object>> whetherInfoList = oneJdbcTemplate
				.queryForList("select * from la_weather order by id desc limit 1");
		Map<String, Object> whetherInfo = Maps.newHashMap();
		if (whetherInfoList.size() > 0) {
			whetherInfo = whetherInfoList.get(0);
		}
		resultLq.setTainqi(String.valueOf(whetherInfo.get("rainsnow")));
		resultLq.setFengxiang(String.valueOf(whetherInfo.get("direction")));
		return ResponseJson.builder().success(true).obj(resultLq).build();
	}

	@ApiOperation(value = "列表")
	@RequestMapping(value = "/LBlist", method = RequestMethod.GET)
	@ResponseBody
	public ResponseJson getLBList(PageBean page, QueryLq queryLq) throws Exception {
		String jcsj = queryLq.getJcsj();
		if (jcsj != null && jcsj.indexOf(" - ") > 0) {
			queryLq.setChecktimebegin(jcsj.split(" - ")[0]);
			queryLq.setChecktimestop(jcsj.split(" - ")[1]);
		}
		Page<ResultLq> pageResult = new Page<ResultLq>();
		List<ResultLq> lqLBs = lqService.queryLqGL(queryLq);
		pageResult.setRecords(lqLBs);
		pageResult.setTotal(lqLBs.size());
		return ResponseJson.builder().success(true).obj(pageResult).build();
	}

	@ApiOperation(value = "汇总")
	@RequestMapping(value = "/HZlist", method = RequestMethod.GET)
	@ResponseBody
	public ResponseJson getHZList(PageBean page, QueryLq queryLq) throws Exception {
		String jcsj = queryLq.getJcsj();
		if (jcsj != null && jcsj.indexOf(" - ") > 0) {
			queryLq.setChecktimebegin(jcsj.split(" - ")[0]);
			queryLq.setChecktimestop(jcsj.split(" - ")[1]);
		}
		queryLq.setJcsj(null);
		Page<ResultLq> pageResult = new Page<ResultLq>();
		Pagination pagination = new Pagination();
		pagination.setCurrent(page.getPageNumber());
		pagination.setSize(page.getPageSize());
		List<ResultLq> lqLBs = lqService.queryLqXQ(queryLq, pagination);
		pageResult.setRecords(lqLBs);
		pageResult.setTotal(pagination.getTotal());
		return ResponseJson.builder().success(true).obj(pageResult).build();
	}

	@ApiOperation(value = "三温图")
	@RequestMapping(value = "/sanwen", method = RequestMethod.GET)
	@ResponseBody
	public ResponseJson getSanwen(QueryLq queryLq) throws Exception {
		List<Map<String, Object>> infoList = oneJdbcTemplate
				.queryForList("select * from la_storehousegraininspection where orgcode = '" + queryLq.getOrgcode()
						+ "' and storehousecode = '" + queryLq.getStorehousecode() + "' order by id desc limit 100");
		return ResponseJson.builder().success(true).obj(infoList).build();
	}

}
